# -*-coding:utf-8
# ProjectName: smart-water-iot-test
# FileName: get_log.py
# @Time :2022/8/29 09:32
# @Author :
# @Description:日志模块

import os
from loguru import logger

from common.util.configUtil.configUtil import ConfigUtil
from common.util.timeUtil.timeUtil import get_now_time


class LoggerUtil:
    def __init__(self, logType: int = 1):
        """logType=1-->mqttlog,logType=2--chargelog"""
        self.type = logType
        self.config = ConfigUtil()
        self.dataBase_environment = self.config.get_database_status()
        self.logPath = self.config.get_log_path(logType)

    def creat_log(self, logName):
        if self.dataBase_environment is True:
            """True：线上环境,False：测试环境"""
            logName = os.path.join(self.logPath, f"pro{get_now_time(3)}-{logName}.log")
            logger.remove(handler_id=None)
            logger.add(logName, retention="7 days", rotation="5MB", enqueue=True, encoding='utf-8', compression="zip",
                       format="{time:YYYY-MM-DD HH:mm:ss} {level} :{message}")
        else:
            logName = os.path.join(self.logPath, f"test-{get_now_time(3)}-{logName}.log")
            logger.remove(handler_id=None)
            logger.add(logName, retention="7 days", rotation="5MB", enqueue=True, encoding='utf-8', compression="zip",
                       format="{time:YYYY-MM-DD HH:mm:ss} {level} :{message}")

    def info(self, msg):
        """打印 info 级别的日志"""
        self.creat_log("info")
        logger.info(msg)

    def debug(self, msg):
        """打印 debug 级别的日志"""
        self.creat_log("debug")
        logger.debug(msg)

    def warning(self, msg):
        """打印 warning 级别的日志"""
        self.creat_log("warning")
        logger.warning(msg)

    def error(self, msg):
        """打印 error 级别的日志"""
        self.creat_log("error")
        logger.error(msg)


if __name__ == '__main__':
    log = LoggerUtil(2)
    log.info("cddfdf")
